/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.classes;

import utsc.apps.utschedule.classes.UofTGrade;

import com.j256.ormlite.field.DatabaseField;

/**
 * This is a generic class that represents a task type object.<br/>
 * Any task type object should extend from this class.
 * 
 * @author 
 * 
 */
public class GenericTask  {

	public static final String WEIGHT="_Weight";
	public static final String MARK="_mark";
	public static final String TOTAL="_total";
	
	@DatabaseField
	private double _Weight;
	@DatabaseField
	private double _mark;
	@DatabaseField
	private double _total;
	
	protected UofTGrade _grade;

	public double get_total() {
		return _total;
	}

	public void set_total(double _total) {
		this._total = _total;
	}
	
	public double get_mark() {
		return _mark;
	}

	public void set_mark(double _mark) {
		this._mark = _mark;
	}

	public UofTGrade get_grade() {
		if (_grade==null) {
			_grade=new UofTGrade(_mark, _total, _Weight);
		}
		return _grade;
	}
	
	public double get_weight() {
		return _Weight;
	}
	
	public void set_weight(double _Weight) {
		this._Weight = _Weight;
	}

	/**
	 * Since we are caching the grade object, so this method would refresh the cache for us.
	 */
	public void refresh_grade(){
		if (_grade!=null) {
			_grade.set_letterGrade(null);
			_grade.set_marks(_mark);
			_grade.set_total(_total);
			_grade.set_weight(_Weight);
			_grade.calculatePercentage();
			_grade.calculatePercentageWeight();
		}else {
			_grade=new UofTGrade(_mark, _total, _Weight);
		}
	}
	
	/**
	 * Directly set the grade object cache, it also refresh the corresponding fields we use to setup the cache (mark, total, weight).
	 * @param _grade the input grade object
	 */
	public void set_grade(UofTGrade _grade) {
		this._grade = _grade;
		_mark=_grade.get_marks();
		_total=_grade.get_total();
	}
	
	/**
	 * Copy the current {@link GenericTask task} to a new reference.
	 * @return
	 */
	public GenericTask copyTask(){
		GenericTask newCopy=new GenericTask();
		newCopy.set_grade(_grade);
		newCopy.set_mark(_mark);
		newCopy.set_total(_total);
		newCopy.set_weight(_Weight);
		return newCopy;
	}
	
}
